﻿<cfsilent>
	<cfsetting requesttimeout="300" />
	<cfscript>
	
		propAdvice = getProperty("serviceFactory").getBean("studentPropertyAdvice");
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		fileObj = getProperty("serviceFactory").getBean("tagFileHelper");
		
		subjectId = event.getArg("SubjectID");
		grade = event.getArg("Grade");
		
		picSuccess = 0;
		picFailure = 0;
		
		response = structNew();
		structInsert(response, "success", false, true);
		
		if ( len(subjectId) and len(grade) ) {
		
			sql = "SELECT 
					a.stu_id
					FROM t_student a  
						INNER JOIN t_class b ON b.cls_id = a.cls_id 
					WHERE 
						b.sbj_id = :subjectId 
						AND 
						b.grade = :grade ";
				
			queryObj = new query( datasource=application.dnsSlave);
			queryObj.addParam( name="subjectId", value=subjectId , cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="grade", value=grade , cfsqltype="cf_sql_char" );
			
			rs_studentIds = queryObj.execute( sql=sql ).getResult();
		
			transaction {

				for (a = 1; a lte rs_studentIds.recordCount; a++) {
			
					portraitDir = getProperty("cdnAssetsPath") & "/student/portrait/" & left( rs_studentIds["stu_id"][a], 4 ) & "/";
					profileDir = getProperty("cdnAssetsPath") & "/student/profile/" & left( rs_studentIds["stu_id"][a], 4 ) & "/";
					printDir = getProperty("cdnAssetsPath") & "/student/print/" & left( rs_studentIds["stu_id"][a], 4 ) & "/";
			
					sql = "SELECT a.student_prop 
							FROM t_student_status a 
							WHERE a.stu_id = :studentId ";
					
					queryObj = new Query( datasource=application.dnsMaster );
					queryObj.addParam( name="studentId", value=rs_studentIds["stu_id"][a], cfsqltype="cf_sql_varchar" );
					
					rs_student = queryObj.execute( sql=sql ).getResult();
				
					propAdvice.parseProperty( rs_student.student_prop );
				
					/* 若各类照片都存在 则刷新学生状态 */
					if ( fileExists( portraitDir & rs_studentIds["stu_id"][a] & ".jpg" ) and fileExists( profileDir & rs_studentIds["stu_id"][a] & ".jpg" ) and fileExists( printDir & rs_studentIds["stu_id"][a] & ".jpg" ) ) {
						
						/* 将图片对象放置到系统缓存 */
						binaryStream = fileObj.readBinary( portraitDir & rs_studentIds["stu_id"][a] & ".jpg" );
						cachePut( "user_portrait_" & rs_studentIds["stu_id"][a], binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
						
						binaryStream = fileObj.readBinary( profileDir & rs_studentIds["stu_id"][a] & ".jpg" );
						cachePut( "user_profile_" & rs_studentIds["stu_id"][a], binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
						
						binaryStream = fileObj.readBinary( printDir & rs_studentIds["stu_id"][a] & ".jpg" );
						cachePut( "user_print_" & rs_studentIds["stu_id"][a], binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
						
						propAdvice.setHasPicture( true );
						
						picSuccess++;
					}
					else {
						propAdvice.setHasPicture( false );
						
						picFailure++;
					}
					
					sql = "UPDATE t_student_status a
							SET a.student_prop = :StudentProps 
							WHERE a.stu_id = :studentId ";
					
					queryObj = new Query( datasource=application.dnsMaster );
					queryObj.addParam( name="studentId", value=rs_studentIds["stu_id"][a], cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="StudentProps", value= propAdvice.stringProperty(), cfsqltype="cf_sql_varchar" );
					
					queryObj.execute( sql=sql );
				
				}
			
			}
			
			structInsert(response, "operation", "本次处理 "& rs_studentIds.recordCount &" 条记录, 签署成功学籍电子照片 "& picSuccess &" 张, 缺失图片 "& picFailure &" 张", true);
			
			structInsert(response, "SubjectID", subjectId);
			structInsert(response, "Grade", grade);
			structInsert(response, "Missing", picFailure);
		}



		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>